package com.kingtone.rongji.network;

import org.apache.http.Header;
import org.apache.http.HttpStatus;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.kingtone.rongji.biz.ProductList;
import com.kingtone.rongji.tool.AppRestClient;
import com.kingtone.rongji.tool.RongJiRestClient;
import com.kingtone.rongji.tool.RongJiUtils;
import com.loopj.android.http.BaseJsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class ProductRequest extends CommonRequest {

	public void findByCommon(String keyWord, int categoryId, int productType,
			int SortOrder, String sortBy, int pageIndex, int pageSize,
			final IRequestHandler restClient, boolean showProgress) {
		RequestParams params = new RequestParams();
		params.put("ProductType", productType);
		params.put("CategoryId", categoryId);
		params.put("SortOrder", SortOrder);
		params.put("IsWebsiteSearch", true);
		params.put("SortBy", sortBy);
		if (keyWord != null && keyWord.trim().length() > 0) {
			params.put("ProductKeyword", keyWord);
		}
		params.put("PageIndex", pageIndex);
		params.put("PageSize", pageSize);
		params.setUseJsonStreamer(true);
		AppRestClient.post(RongJiRestClient.PRODUCTS_COMMON_URL, params, new ProductHandler(
				restClient, showProgress));
	}

	class ProductHandler extends BaseJsonHttpResponseHandler {

		private IRequestHandler restClient;
		
		private boolean showProgress;

		public ProductHandler(IRequestHandler restClient, boolean showProgress) {
			super();
			this.restClient = restClient;
			this.showProgress = showProgress;
		}

		@Override
		public void onStart() {
			if(showProgress)
			{
				RongJiUtils.showEmptyProgress(restClient.getContext());
			}
		}
		
		@Override
		public void onFailure(int arg0, Header[] arg1, Throwable throwable,
				String arg3, Object arg4) {
			// TODO Auto-generated method stub
			if(showProgress)
			{
				RongJiUtils.dismissProgress();
			}
			if (null != throwable) {
				throwable.printStackTrace();
			}
			restClient.onNetworkFailure();
		}

		@Override
		public void onSuccess(int statusCode, Header[] arg1,
				String responseString, Object arg3) {
			// TODO Auto-generated method stub
			if(showProgress)
			{
				RongJiUtils.dismissProgress();
			}
			if (statusCode == HttpStatus.SC_OK) {
				try {
					ProductList products = parseResponse(responseString, false);
					if (products != null) {
						restClient.onBussinessSuccess(products);
						return;
					}
				} catch (Throwable e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			restClient.onNetworkFailure();
		}

		@Override
		protected ProductList parseResponse(String rawJsonData,
				boolean isFailure) throws Throwable {
			// TODO Auto-generated method stub
			Gson gson = new Gson();
			JsonParser parser = new JsonParser();
			JsonElement elment = parser.parse(rawJsonData);
			ProductList products = gson.fromJson(elment, ProductList.class);
			return products;
		}

	}
}
